package Practice1;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author d11yu
 */
public class HelloServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("别名："+servletConfig.getServletName());
        System.out.println("username的值：" + servletConfig.getInitParameter("username"));
        System.out.println("url的值：" + servletConfig.getInitParameter("url"));
        System.out.println("context：" + servletConfig.getServletContext());
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * service方法是专门用来处理请求和响应的
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Hello Servlet被访问了！");
        HttpServletRequest hsRequest = (HttpServletRequest) servletRequest;
        String method = hsRequest.getMethod();
        if (method.equals("GET")) {
            doGet();
        } else if (method.equals("POST")) {
            doPost();
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }

    /**
     * get请求的操作
     */
    public void doGet() {
        System.out.println("get请求");
    }

    /**
     * post请求的操作
     */
    public void doPost() {
        System.out.println("post请求");
    }
}
